package core;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import network.ClientListener;

/**
 * @author Peter Nikodem
 */
public class ContactImpl implements Contact {

    private List<String> messageQueue = new LinkedList<String>();
    private List<ClientListener> listeners = new ArrayList<ClientListener>();
    private List<Contact> subscribers = new ArrayList<Contact>();
    private final String user;
    private final String pass;

    public ContactImpl(String user, String pass) {
        this.user = user;
        this.pass = pass;
    }

    @Override
    public void addMessage(String message) {
        synchronized (messageQueue) {
            messageQueue.add(message);
            notifyListeners();
        }
    }

    public String getNextMessage() {
        synchronized (messageQueue) {
            return messageQueue.remove(0);
        }
    }

    @Override
    public void sendPresence(String message) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void addListener(ClientListener lst) {
        synchronized (listeners) {
            listeners.add(lst);
        }

    }

    private void notifyListeners() {
        synchronized (listeners) {
            for (ClientListener lst : listeners) {
                listeners.notify();
            }
        }
    }
}
